// 题目生成配置
const problemConfig = {
    1: { // 一年级
        count: 10,
        range: [1, 20],
        operators: ['+', '-']
    },
    2: { // 二年级
        count: 12,
        range: [1, 100],
        operators: ['+', '-', '×']
    },
    3: { // 三年级
        count: 15,
        range: [1, 100],
        operators: ['+', '-', '×', '÷']
    }
};

let currentProblems = [];
let score = { correct: 0, total: 0 };

document.getElementById('grade-select').addEventListener('change', generateProblems);
document.getElementById('submit-all').addEventListener('click', checkAllAnswers);

// 生成题目
function generateProblems() {
    const grade = parseInt(document.getElementById('grade-select').value);
    const config = problemConfig[grade];
    const container = document.getElementById('problems-container');
    
    container.innerHTML = '';
    currentProblems = [];
    score = { correct: 0, total: config.count };

    for (let i = 0; i < config.count; i++) {
        const { problem, answer } = createProblem(config);
        currentProblems.push({ id: i, answer });
        
        const problemEl = document.createElement('div');
        problemEl.className = 'problem-item';
        problemEl.innerHTML = `
            <div class="problem-text">${i + 1}. ${problem} =</div>
            <input type="number" step="0.1" id="answer-${i}" placeholder="输入答案">
        `;
        container.appendChild(problemEl);
    }
    updateScore();
}

// 创建单个题目
function createProblem(config) {
    const [min, max] = config.range;
    const a = Math.floor(Math.random() * (max - min + 1)) + min;
    const b = Math.floor(Math.random() * (max - min + 1)) + min;
    const op = config.operators[Math.floor(Math.random() * config.operators.length)];

    let problem, answer;
    switch(op) {
        case '+':
            problem = `${a} + ${b}`;
            answer = a + b;
            break;
        case '-':
            problem = `${Math.max(a, b)} - ${Math.min(a, b)}`;
            answer = Math.abs(a - b);
            break;
        case '×':
            problem = `${a} × ${b}`;
            answer = a * b;
            break;
        case '÷':
            const divisor = b !== 0 ? b : 1;
            const multiple = a * divisor;
            problem = `${multiple} ÷ ${divisor}`;
            answer = a;
            break;
    }
    return { problem, answer };
}

// 检查所有答案
function checkAllAnswers() {
    let correctCount = 0;
    currentProblems.forEach(({ id, answer }) => {
        const input = document.getElementById(`answer-${id}`);
        const userAnswer = parseFloat(input.value);
        const problemEl = input.closest('.problem-item');
        
        problemEl.classList.remove('correct', 'wrong', 'missing');
        
        if (isNaN(userAnswer)) {
            problemEl.classList.add('missing');
        } else if (Math.abs(userAnswer - answer) < 0.01) {
            problemEl.classList.add('correct');
            correctCount++;
        } else {
            problemEl.classList.add('wrong');
        }
    });

    score.correct = correctCount;
    updateScore();
}

// 更新分数显示
function updateScore() {
    const scoreEl = document.getElementById('score');
    scoreEl.textContent = `得分：${score.correct}/${score.total} (${Math.round((score.correct/score.total)*100)}%)`;
}
